package com.suray.rgv60.generate;

import com.alibaba.fastjson.JSON;
import com.suray.basic.wcs.path.core.Node;
import com.suray.basic.wcs.utils.Coord;
import com.suray.basic.wcs.utils.LoggerType;
import com.suray.basic.wcs.utils.WcsFuncUtil;
import com.suray.rgv.util.RgvLogger;
import com.suray.type.RgvMapTagDirection;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生成地图句柄
 */
public class MapMsgManageHandle {

    //获取区域
    public Map<Integer,List<Rgv60MapStruct.Scope>> getAdvancedAreaList(String paperFile) throws IOException {
        Map<Integer,List<Rgv60MapStruct.Scope>> advancedAreaListMap = new HashMap<>();
        paperFile += File.separator +  "wcs_area";
        File parent = new File(paperFile);
        if(parent.listFiles() == null){
            return advancedAreaListMap;
        }
        for (File file : parent.listFiles()) {
            String smapScope = WcsFuncUtil.readFile(file.getPath());
            String layer  = file.getName().replace(".area", "").split("_")[1];
            advancedAreaListMap.put(Integer.valueOf(layer), JSON.parseArray(smapScope,Rgv60MapStruct.Scope.class));
        }
        return advancedAreaListMap;
    }

    //生成地图信息
    public void generalMapMsg(String paperFile, List<Node> mapNodes, RgvMapTagDirection direction,
                                 Map<Integer, List<Rgv60MapStruct.Scope>> advancedAreaListMap) {
        Map<Integer, List<Node>> floorNodes = new HashMap<>();
        mapNodes.forEach(node -> {
            List<Node> nodes = floorNodes.get(node.getZ());

            if (CollectionUtils.isEmpty(nodes)) {
                nodes = new ArrayList<>();
                floorNodes.put(node.getZ(), nodes);
            }

            nodes.add(node);
        });

        floorNodes.keySet().forEach(floor -> {
            try {
                List<Rgv60MapStruct.Scope> advancedAreaList = advancedAreaListMap.get(floor);
                GeneralMapManage.generalMap(paperFile,floor, "", floorNodes.get(floor),
                        direction, advancedAreaList);
            } catch (Throwable e) {
                RgvLogger.exceptionLog(LoggerType.ERROR, floor, paperFile, e, this.getClass());
            }
        });
    }

    //获取X轴相邻点位移
    public void analyzeUp(Node node, Rgv60MapStruct.TagPosListDTO tagPosListDTO,
                           Map<Integer, Rgv60MapStruct.TagPosListDTO> tagPosMap, Map<Coord, Node> coordNodeMap) {
        if (node.getX() > 1) {
            Node upNode = coordNodeMap.get(new Node(node.getX() - 1, node.getY(), node.getZ()));
            Rgv60MapStruct.TagPosListDTO upTagPosListDTO = tagPosMap.get(upNode.getGroundCode());
            if (upTagPosListDTO != null)  {
                node.setOffsetX((int)((upTagPosListDTO.getY() - tagPosListDTO.getY()) * 1000));
            }
        }
    }

    //获取Y轴相邻点位移
    public void analyzeLeft(Node node, Rgv60MapStruct.TagPosListDTO tagPosListDTO,
                             Map<Integer, Rgv60MapStruct.TagPosListDTO> tagPosMap, Map<Coord, Node> coordNodeMap) {
        if (node.getY() > 1) {
            Node leftNode = coordNodeMap.get(new Node(node.getX(), node.getY() - 1, node.getZ()));
            Rgv60MapStruct.TagPosListDTO leftTagPosListDTO = tagPosMap.get(leftNode.getGroundCode());
            if (leftTagPosListDTO != null)  {
                node.setOffsetY((int)((tagPosListDTO.getX() - leftTagPosListDTO.getX()) * 1000));
            }
        }
    }
}
